package controllers

import java.io.File

import dao._
import javax.inject.Inject
import models.Tables._
import play.api.mvc.{AbstractController, Action, ControllerComponents}
import utils.Utils

import scala.annotation.tailrec
import scala.io.Source
import scala.xml.Utility
import scala.xml.pull.{EvElemEnd, EvElemStart, EvEntityRef, EvText, XMLEvent, XMLEventReader}
import tool.Tool
import implicits.Implicits._
import org.joda.time.DateTime
import play.api.libs.json.Json
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by Administrator on 2019/8/14
 */
class DataInsertController @Inject()(cc: ControllerComponents, posSiteDao: PosSiteDao) extends AbstractController(cc) {

  def insertPosSite = Action.async { implicit request =>
    val strFile = new File(Tool.dataDir, "str.xlsx")
    val strLines = strFile.xlsxLines().drop(1)
    val femaleStrMap = strLines.map { columns =>
      columns(0) -> columns(1)
    }.toSeqMap
    val strFemaleRow = PosSiteRow("女", "str", Json.toJson(femaleStrMap).toString())
    val strMaleMap = strLines.map { columns =>
      columns(0) -> columns(2)
    }.toSeqMap
    val strMaleRow = PosSiteRow("男", "str", Json.toJson(strMaleMap).toString())
    val snpFile = new File(Tool.dataDir, "snp.xlsx")
    val snpLines = snpFile.xlsxLines().drop(1)
    val femaleSnpMap = snpLines.map { columns =>
      columns(0) -> columns(1)
    }.toSeqMap
    val snpFemaleRow = PosSiteRow("女", "snp", Json.toJson(femaleSnpMap).toString())
    val maleSnpMap = snpLines.map { columns =>
      columns(0) -> columns(2)
    }.toSeqMap
    val snpMaleRow = PosSiteRow("男", "snp", Json.toJson(maleSnpMap).toString())
    val rows = List(strMaleRow, strFemaleRow, snpMaleRow, snpFemaleRow)
    posSiteDao.insertOrUpdates(rows).map { x =>
      Ok("success!")
    }
  }


}
